#include "vtor_timer.h"

#ifdef __VTOR_TIMER__

// 一直指向所有任务的头，不可更改
static VtorTimer* VtorTimer_arrayHeader = NULL;
static uint32_t VtorTimer_tick = 0;
static uint32_t VtorTimer_cnt = 0;


void VtorTimer_VoidFun()
{
	
}

void VtorTimer_Schedule(void)
{
	VtorTimer* timer = VtorTimer_arrayHeader;
	if(NULL == timer)
	{
		return;
	}
	VtorTimer_cnt++;
	while (NULL != timer->fun)
	{
		if (VtorTimer_VoidFun != timer->fun)
		{
			if(VTOR_TIMER_STATE_READY == timer->state)
			{
				uint32_t endTime = 0;
				uint32_t startTime = VtorTimer_GetTick();
				timer->state = VTOR_TIMER_STATE_RUNNING;
				timer->fun();
				if(VTOR_TIMER_STATE_RUNNING == timer->state)
				{
					timer->state = VTOR_TIMER_STATE_WAITING;
				}
				timer->runCnt++;
				endTime = VtorTimer_GetTick();
				timer->runTime = endTime - startTime;
			}
		}
		timer++;
	}
}


void VtorTimer_IncTick(uint32_t tick)
{
	VtorTimer* timer = VtorTimer_arrayHeader;
	if(NULL == timer)
	{
		return;
	}
	VtorTimer_tick += tick;
	while (NULL != timer->fun)
	{
		if (VtorTimer_VoidFun != timer->fun)
		{
			if(VTOR_TIMER_STATE_WAITING == timer->state)
			{
				timer->curTime += tick;
				if (timer->curTime >= timer->interval)
				{
					timer->curTime = 0;
					timer->state = VTOR_TIMER_STATE_READY;
				}
			}
		}
		timer++;
	}
	VtorTimer_IncTickCallback(tick);
}

uint32_t VtorTimer_GetTick(void)
{
	return VtorTimer_tick;
}

uint32_t VtorTimer_GetCnt(void)
{
	return VtorTimer_cnt;
}

void VtorTimer_SetArrayHeader(VtorTimer* header)
{
	VtorTimer_arrayHeader = header;
}
VtorTimer* VtorTimer_GetArrayHeader()
{
	return VtorTimer_arrayHeader;
}

VtorTimer* VtorTimer_GetRunningTimer()
{
	VtorTimer* timer = VtorTimer_arrayHeader;
	if (NULL == timer)
	{
		return NULL;
	}
	while (NULL != timer->fun)
	{
		if(VTOR_TIMER_STATE_RUNNING == timer->state)
		{
			return timer;
		}
		timer++;
	}
	return NULL;
}

VtorTimer* VtorTimer_GetTimerByFun(VtorTimerFun fun)
{
	VtorTimer* timer = VtorTimer_arrayHeader;
	if (NULL == timer || NULL == fun)
	{
		return NULL;
	}
	while(NULL != timer->fun)
	{
		if (fun == timer->fun)
		{
			return timer;
		}
		timer++;
	}
	return NULL;
}

VtorTimer* VtorTimer_Create(VtorTimerFun fun, uint32_t interval)
{
	VtorTimer* timer = NULL;
	timer = VtorTimer_GetTimerByFun(fun);
	if(NULL == timer)
	{
		timer = VtorTimer_GetTimerByFun(VtorTimer_VoidFun);
		if(NULL == timer)
		{
			return NULL;
		}
		timer->fun = fun;
		VtorTimer_CreateCallback(timer->fun);
	}
	timer->curTime = 0;
	timer->interval = interval;
	timer->state = VTOR_TIMER_STATE_WAITING;
	return timer;
}


VtorTimer* VtorTimer_Start(VtorTimerFun fun, uint32_t interval)
{
	VtorTimer* timer = NULL;
	timer = VtorTimer_GetTimerByFun(fun);
	if(NULL != timer)
	{
		timer->state = VTOR_TIMER_STATE_WAITING;
		timer->interval = interval;
	}
	return timer;
}

void VtorTimer_RunNow(VtorTimerFun fun)
{
	VtorTimer* timer = NULL;
	timer = VtorTimer_GetTimerByFun(fun);

	if(NULL != timer)
	{
		timer->state = VTOR_TIMER_STATE_READY;
		timer->curTime = 0;
	}
}

void VtorTimer_Delete(VtorTimerFun fun)
{
	VtorTimer* timer = NULL;
	timer = VtorTimer_GetTimerByFun(fun);
	if(NULL != timer)
	{
		if (VTOR_TIMER_STATE_STOP != timer->state)
		{
			VtorTimer_DeleteCallback(fun);
			timer->state = VTOR_TIMER_STATE_STOP;
			timer->fun = VtorTimer_VoidFun;
		}
		timer->runCnt = 0;
		timer->curTime = 0;
	}

}

void VtorTimer_Stop(VtorTimerFun fun)
{
	VtorTimer* timer = NULL;
	timer = VtorTimer_GetTimerByFun(fun);
	if(NULL != timer)
	{
		timer->state = VTOR_TIMER_STATE_STOP;
		timer->curTime = 0;
	}
}

#endif // __VTOR_TIMER__

